/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using IQIGame.Onigao.Framework;
using System.Collections.Generic;
using System.Linq;
using System;
using NetProtocol.Enum;

namespace NetProtocol.POD {

	/// <summary>
	/// 
	/// module : battleLogic
	/// </summary>
	public class BattleEnterPOD : IPOD 
    {
        public readonly static Func<BattleEnterPOD> FACTORY = () => new BattleEnterPOD();
		public static Func<BattleEnterPOD> DynamicFactory = FACTORY;
        
		/*-- members --*/
		/// <summary>
        /// 
        /// </summary>
		public bool isLocal;
		/// <summary>
        /// 
        /// </summary>
		public BattleType battleType;
		/// <summary>
        /// 
        /// </summary>
		public long battleId;
		/// <summary>
        /// 
        /// </summary>
        private List<BattleTeamViewPOD> m_teams;
		/// <summary>
        /// 
        /// </summary>
		public int battleScene;
		/// <summary>
        /// 
        /// </summary>
		public int randomSeed;
		/// <summary>
        /// 
        /// </summary>
		public BattleUnitViewPOD subsitute;
		
		
        public List<BattleTeamViewPOD> teams
        {
            get
            {
                if(m_teams == null)
                {
                    m_teams = new();
                }
                return m_teams;
            }
            private set => m_teams = value;
        }
        public List<BattleTeamViewPOD> unsafe_teams
        {
        	get => m_teams;
        	set => m_teams = value;
        }
		
		/*-- constructors --*/
		public BattleEnterPOD() {}
		public BattleEnterPOD(bool isLocal, BattleType battleType, long battleId, List<BattleTeamViewPOD> teams, int battleScene, int randomSeed, BattleUnitViewPOD subsitute) {
			this.isLocal = isLocal;
			this.battleType = battleType;
			this.battleId = battleId;
			this.m_teams = teams;
			this.battleScene = battleScene;
			this.randomSeed = randomSeed;
			this.subsitute = subsitute;
		}
	      public void Serial(SOutputStream _out) {
            _out.Write_boolean(isLocal);
            _out.Write_byte((sbyte)battleType);
            _out.Write_long(battleId);
            _out.Write_list_pod(m_teams);
            _out.Write_int(battleScene);
            _out.Write_int(randomSeed);
            _out.Write_pod(subsitute);
        }
        
        public void UnSerial(SInputStream _in) {
            if(!_in.HasRemaining()) return;
            isLocal = _in.Read_boolean();
            if(!_in.HasRemaining()) return;
            battleType = (BattleType)_in.Read_byte();
            if(!_in.HasRemaining()) return;
            battleId = _in.Read_long();
            if(!_in.HasRemaining()) return;
            m_teams = _in.Read_list_pod<BattleTeamViewPOD>(BattleTeamViewPOD.DynamicFactory);
            if(!_in.HasRemaining()) return;
            battleScene = _in.Read_int();
            if(!_in.HasRemaining()) return;
            randomSeed = _in.Read_int();
            if(!_in.HasRemaining()) return;
            subsitute = _in.Read_pod<BattleUnitViewPOD>(BattleUnitViewPOD.DynamicFactory);
        }
		
		public BattleEnterPOD Clone() {
            BattleEnterPOD o = DynamicFactory();
            o.isLocal = isLocal;
            o.battleType = battleType;
            o.battleId = battleId;
            if (m_teams != null) {
                o.m_teams = m_teams.ConvertAll(o => o?.Clone());
            }
            o.battleScene = battleScene;
            o.randomSeed = randomSeed;
            if (subsitute != null) {
                o.subsitute = subsitute.Clone();
            }
            return o;
        }
        
        public void Clear() {
            isLocal = default;
            battleType = default;
            battleId = default;
            m_teams = default;
            battleScene = default;
            randomSeed = default;
            subsitute = default;
        }
		
        public override string ToString() {
			string str = "";
			str += "BattleEnterPOD [";
			str += "isLocal=";
			str += isLocal;
			str += ", battleType=";
			str += battleType;
			str += ", battleId=";
			str += battleId;
			str += ", teams=";
			str += m_teams.ToString<BattleTeamViewPOD>();
			str += ", battleScene=";
			str += battleScene;
			str += ", randomSeed=";
			str += randomSeed;
			str += ", subsitute=";
			str += subsitute;
			str += "]";
			return str;
		}
		
	}
}